<?php
/**
 * 公共控制器
 * User: Administrator
 * Date: 2018/10/29 0029
 * Time: 20:55
 */

namespace app\wx\controller;
//use library\Helpers;
use think\Cache;
use think\Controller;
use think\Session;
use exception\ResponsableException;
use  library\Helpers;
use module\UserApi;

class CommonController extends Controller
{

    const SUCCESS_CODE = 200; // 系统成功代码
    const ERROR_CODE = 400; // 失败代码
    const  NOT_LOGIN = 4004;//用户未登录
    // 事件处理状态(1表示等待处理,2正常处理完毕,3处理完毕,业务出现异常,4未正常处理完毕)
    const STATUS_START = 1;
    const STATUS_FINISH = 2;
    const STATUS_ERROR = 3;
    const STATUS_EXCEPTION = 4;

    /**
     * @var int 用户登录是否互踢
     */
    private $kick_device_type = 0;

    /**
     * @var \think\Request 请求体
     */
    var $request;

    /**
     * @var \library\Response 响应体
     */
    var $response;

    /**
     * @var boolean 是否强制登录
     */
    protected $_require_login = TRUE;
    protected $uid;


    /**
     * 构造方法
     * @throws
     */
    public function __construct(\think\Request $request)
    {
        header('Access-Control-Allow-Methods:OPTIONS, GET, POST');
        header('Access-Control-Allow-Headers:x-requested-with');
        header('Access-Control-Max-Age:86400');
        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        header('Access-Control-Allow-Headers:Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, authorization');
        parent::__construct($request);
        $this->request = $request;


        if (isset($this->require_login))
            $this->_require_login = $this->require_login;

        //开启登录验证
        if ($this->_require_login === TRUE && !session('user_info')) {
          //  throw new ResponsableException(null, ResponsableException::ACCOUNT_USER_NOT_LOGIN);
            $this->redirect('wx/Login/login');
            return;
        }
        $this->response = new \library\Response();
        \think\Request::instance()->bind('_sql', []);
        $list = [];
        if (config('APP_DEBUG') == true) {
            \think\Db::listen(function ($sql, $time) use (&$list) {
                if ('SHOW' == substr($sql, 0, 4)) return;
                array_push($list, $sql);
                \think\Request::instance()->_sql = $list;
            });
        }
       $this->uid = session('user_info')['ui_id'];

    }

    public function __destruct()
    {
        unset($this->request->_sql);
    }


    /**
     * HTTP Basic AUTHORIZATION
     * @author zhc
     * @param int $user_id 用户ID
     *            int  -> generate authorization_token
     *            void -> set $this->user_id $this->uid
     *   $is_login ->         -1   -> logout
     * @return string|int|null
     * @throws ResponsableException
     */
    protected function authorization($user_id = null, $is_login = 1)
    {
        $tokenExpireTime = 30 * 24 * 60 * 60; // 30天

        if ($user_id !== null && is_numeric($user_id)) { // 设置Auth信息
            if ($is_login === -1) { // 清除token信息,退出
                UserApi::updateUser(['ui_id' => $user_id], ['token' => NULL, 'time_out' => NULL]);
                return null;
            }
            // 设置Auth
            $token = Helpers::randomString(64);
            UserApi::updateUser(['ui_id' => $user_id], ['token' => $token, 'time_out' => $tokenExpireTime]);
            return $token;
        }
        $token = $this->request->header('token');
        $user_id = '';
        $user_info = UserApi::getUserInfoByToken($token);
        if ($user_info) {
            $user_id = $user_info['ui_id'];
        }
        // 用户测试
        if (config('APP_ENV') != 'prod') {

            if (!$user_id || !is_numeric($user_id)) $user_id = $this->request->post('user_id', null);
            if (!$user_id || !is_numeric($user_id)) $user_id = $this->request->post('uid', null);
            if (!$user_id || !is_numeric($user_id)) $user_id = $this->request->get('user_id', null);
            if (!$user_id || !is_numeric($user_id)) $user_id = $this->request->get('uid', null);
        }
        if ($user_id) {
            $this->user_id = $this->uid = $user_id;
            return $user_id;
        }
        return null;
    }


    /**
     * 阻止用户相关操作(黑名单用户)
     * @param int $uid 用户uid
     * @param string $authorization 用户Token值
     * @throws ResponsableException
     */
    protected function preventBlackListUser($uid, $authorization = null)
    {
        if (UserApi::queryUser($uid)['status'] == 2) { // 用户已锁定
            if (!is_null($authorization)) {
                Cache::rm('auth:' . $authorization);
            }
            session(null);
            throw new ResponsableException(null, ResponsableException::BLACK_LIST_USER);
        }
    }

    /**
     * 根据状态返回保单状态
     * @param $status_id
     * @return string
     */
    protected function statusChangeWord($status_id)
    {
        $str = '';
        switch ($status_id){
            case 1:
                $str = '未递交审核';
                break;
            case 2:
                $str = '代录人未递交';
                break;
            case 3:
                $str = '代录资料未确认';
                break;
            case 4:
                $str = '待审核';
                break;
            case 5:
                $str = '被保险人未确认';
                break;
            case 6:
                $str = '被保险人未委托';
                break;
            case 7:
                $str = '全部资料已完成';
                break;
            case 8:
                $str = '拍卖已完成';
                break;
            default :
                $str = '驳回修改';
                break;
        }
        return $str;
    }


    /**
     * 返回保单险中
     * @param $type_id
     * @return string
     */
    public function getInsurantType($type_id)
    {
        $str = '';
        switch ($type_id){
            case 1:
                $str = '企财险';
                break;
            case 2:
                $str = '船舶险';
                break;
            case 3:
                $str = '责任险';
                break;
            case 4:
                $str = '工程险';
                break;
            case 5:
                $str = '保证险';
                break;
            case 6:
                $str = '家财险';
                break;
            case 7:
                $str = '其他险';
                break;
        }
        return $str;
    }


    /**
     * 返回保单险中
     * @param $type_id
     * @return string
     */
    public function getGoodType($type_id)
    {
        $str = '';
        switch ($type_id){
            case 1:
                $str = '化工类';
                break;
            case 2:
                $str = '纤维类';
                break;
            case 3:
                $str = '农产品';
                break;
            case 4:
                $str = '设备类';
                break;
            case 5:
                $str = '车辆类';
                break;
            case 6:
                $str = '其他';
                break;
        }
        return $str;
    }


    /**
     * 代录人状态
     * @param $type_id
     * @return string
     */
    public function recoderStatus($type_id)
    {
        $str = '';
        switch ($type_id){
            case 0:
                $str = '未录入';
                break;
            case 1:
                $str = '未递交';
                break;
            case 2:
                $str = '已提交保险公司人员未确认';
                break;
            case 3:
                $str = '驳回';
                break;
            case 4:
                $str = '已完成';
                break;
        }
        return $str;
    }


    /**
     * 被保险人状态
     * @param $status
     * @return string
     */
    public function remerStatus($status)
    {
        $str = '';
        switch ($status){
            case 0:
                $str = '资料未确认';
                break;
            case 1:
                $str = '未递交';
                break;
            case 2:
                $str = '待审核';
                break;
            case 3:
                $str = '驳回';
                break;
            case 4:
                $str = '已完成';
                break;
        }
        return $str;
    }
}
